package com.tjetc.service.impl;

import com.tjetc.common.Resp;
import com.tjetc.dao.AppointmentMapper;
import com.tjetc.dao.RoomMapper;
import com.tjetc.entity.Appointment;
import com.tjetc.entity.Room;
import com.tjetc.service.AppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AppointmentServiceImpl implements AppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    public Resp insertNewAppointment(Appointment appointment) {
        Room room = roomMapper.getRoomById(appointment.getRoomId());
        List<Integer> roomAllId = roomMapper.getAllRoomId();
        List<Map<String,String>> mapList = new ArrayList<>();
        LocalDate date = LocalDate.now();
        String time = date + " 08:00:00";
        //差一个满员
        if(room.getStudentAccount()+1 == room.getMaxAccount()){
            //自习室状态设为满员
            room.setIsFull(1);
            roomMapper.update(room);
            return getResp(appointment, roomAllId, mapList, time);
            //已经满员
        }else if(room.getIsFull() == 1){
            return Resp.error().data("status","预约失败！");
            //离满员还早
        }else {
            return getResp(appointment, roomAllId, mapList, time);
        }
    }

    private Resp getResp(Appointment appointment, List<Integer> roomAllId, List<Map<String, String>> mapList, String time) {
        int isSuc = appointmentMapper.insert(appointment);
        if(isSuc == 1){
            roomAllId.forEach(id->{
                mapList.add(roomMapper.getRoomByIdAndTime(id,time));
            });
            return Resp.ok().data("roomList",mapList).data("status","预约成功！");
        }
        else
            return Resp.error().data("status","预约失败！");
    }

    @Override
    public Resp getAppointRoomByStuId(int userId) {
        LocalDate date = LocalDate.now();
        LocalDate newDate = date.plusDays(1);
        String time = newDate + " 08:00:00";
        List<Integer> list = appointmentMapper.getAppointRoomByStuIdAndTime(userId,time);
        if(!list.isEmpty()){
            return Resp.ok().data("appointRoom",list);
        }
        return Resp.error();
    }

    @Override
    public Resp getMyAppointByUserId(int userId) {
        List<Appointment> list = appointmentMapper.getMyAppointByUserId(userId);
        List<Integer> roomName = new ArrayList<>();
        list.forEach(appoint -> roomName.add(roomMapper.getRoomById(appoint.getRoomId()).getRoomName()));
        return Resp.ok().data("appointList",list).data("roomName",roomName);
    }

    @Override
    public Resp getAllDisagreeAppoint() {
        List<Map<String,String>> appointmentList = appointmentMapper.getAllDisagreeAppoint();
        return Resp.ok().data("appointList",appointmentList);
    }

    @Override
    public Resp disposeAppoint(String option,int id) {
        int state = 0;
        if(option.equals("agree"))
            //同意预约
            state = 1;
            //拒绝预约
        else state = 2;
        int isSuc = appointmentMapper.disposeAppointById(state,id);
        if(isSuc == 1){
            return Resp.ok().data("status","操作完毕！");
        }
        return Resp.error();
    }

    @Override
    public Resp teaGetRoom(int id) {
        int roomId = roomMapper.getRoomIdByTeaId(id);
        LocalDate date = LocalDate.now();
        String time = date + " 08:00:00";
        List<Map<String,String>> mapList;
        mapList = appointmentMapper.teaGetRoom(roomId,time);
        return Resp.ok().data("stuList",mapList);
    }

    @Override
    public Resp getAllAppointment() {
        List<Map<String,String>> mapList;
        mapList = appointmentMapper.getAllAppoint();
        return Resp.ok().data("appointList",mapList);
    }
}
